Plot duplications in tree

Author

Claudia Zirión-Martínez

Published

February 13, 2025

Setup

Libraries

Code
library(tidyverse)
library(ggtree)
library(ggtreeExtra)
library(ape)
library(ggnewscale)
library(RColorBrewer)
library(svglite)
source("scripts/metadata_colors.R")

Paths

Code
metadata_path <- 
    "data/processed/metadata_ashton_desj_all_weavepop_final_H99.csv"
chrom_metrics_path <-
    "results/tables/chromosome_cnv_categories.tsv"
merged_tree_path <- 
    "data/processed/tree_merged.newick"
tree_merged_duplications_path1 <- 
    "results/trees_dups/tree_duplications_full1.png"
tree_merged_duplications_path2 <- 
    "results/trees_dups/tree_duplications_full2.png"
tree_merged_duplications_path3 <- 
    "results/trees_dups/tree_duplications_full3.png"
tree_merged_duplications_path4 <- 
    "results/trees_dups/tree_duplications_full4.png"
tree_merged_duplications_path5 <- 
    "results/trees_dups/tree_duplications_full5.png"
tree_merged_duplications_path6 <- 
    "results/trees_dups/tree_duplications_full6.png"
tree_merged_duplications_path7 <- 
    "results/trees_dups/tree_duplications_full7.png"
tree_merged_duplications_path8 <- 
    "results/trees_dups/tree_duplications_full8.png"
tree_merged_duplications_small1 <-  
    "results/trees_dups/tree_duplications_small1.png"
tree_merged_duplications_small2 <-  
    "results/trees_dups/tree_duplications_small2.png"
tree_merged_duplications_small3 <-  
    "results/trees_dups/tree_duplications_small3.png"
tree_merged_duplications_small4 <-  
    "results/trees_dups/tree_duplications_small4.svg"
tree_merged_duplications_small5 <-  
    "results/trees_dups/tree_duplications_small5.png"
tree_merged_duplications_small6 <-  
    "results/trees_dups/tree_duplications_small6.png"
tree_merged_duplications_small7 <-  
    "results/trees_dups/tree_duplications_small7.png"
tree_merged_duplications_small8 <-  
    "results/trees_dups/tree_duplications_small8.png"
tree_merged_duplications_small9 <-  
    "results/trees_dups/tree_duplications_small9.png"
tree_merged_duplications_small10 <-  
    "results/trees_dups/tree_duplications_small10.png"

Metadata

Load the necessary data

Code
metadata <- read.csv(
    metadata_path,
    header = TRUE)%>%
    select(strain, everything())

Get one dataframe for each variable to be plotted as a separate metadata column in the tree

Code
metadata$vni_subdivision <- factor(metadata$vni_subdivision,
                            levels = c("VNIa-4", "VNIa-5", "VNIa-32", 
                            "VNIa-93", "VNIa-X", "VNIa-Y", "VNIb", 
                            "VNIc", "VNIa-outlier"))
metadata$country_of_origin <- factor(metadata$country_of_origin,
                                levels = names(country_colors))
metadata$continent <- factor(metadata$continent,
                                levels = names(continent_colors))            

sublineage <- metadata %>%
                filter(lineage == "VNI")%>%
                select(strain, vni_subdivision)%>%
                column_to_rownames("strain")%>%
                droplevels()
lineage <- metadata %>%
            select(strain, lineage)%>%
            column_to_rownames("strain")
dataset <- metadata %>%
            select(strain, dataset)%>%
            column_to_rownames("strain")
source <- metadata %>%
            select(strain, source)%>%
            column_to_rownames("strain")
country <- metadata %>%
            select(strain, country_of_origin)%>%
            column_to_rownames("strain")     
continent <- metadata %>%
            select(strain, continent)%>%
            column_to_rownames("strain")  

CNV categories and metrics per chromosome

Code
chrom_metrics <- read.delim(
    chrom_metrics_path,
    header = TRUE,
    sep = "\t"
)
chrom_metrics$chrom_category_aneuploidy <- factor(chrom_metrics$chrom_category_aneuploidy, 
                levels = c("Full", "Partial", "Euploid"))

chrom_metrics$chrom_category_size <- factor(chrom_metrics$chrom_category_size, 
                levels = c("Large", "Medium", "Small", "Absent"))

Make matrix of coverage percent per chromosome for duplications and deletions

Code
dup_coverage <- chrom_metrics %>%
    filter(cnv == "duplication")%>%
    select(strain, chromosome, coverage_percent)%>%
    pivot_wider(names_from = chromosome, values_from = coverage_percent)%>%
    column_to_rownames("strain") 

Make matrix of aneuploidy category per chromosome for duplications

Code
dup_category_aneuploidy <- chrom_metrics %>%
    filter(cnv == "duplication")%>%
    select(strain, chromosome, chrom_category_aneuploidy)%>%
    pivot_wider(names_from = chromosome, values_from = chrom_category_aneuploidy)%>%
    column_to_rownames("strain") 

Make matrix of size category per chromosome for duplications

Code
dup_category_size <- chrom_metrics %>%
    filter(cnv == "duplication")%>%
    select(strain, chromosome, chrom_category_size)%>%
    pivot_wider(names_from = chromosome, values_from = chrom_category_size)%>%
    column_to_rownames("strain") 

Matrix of fully duplicated chromosomes

Code
dup_chroms <- chrom_metrics %>%
    filter(cnv == "duplication")%>%
    select(strain, chromosome, chrom_category_aneuploidy)%>%
    mutate(chrom_category_aneuploidy = as.character(chrom_category_aneuploidy))%>%
    mutate(chromosome_2 = ifelse(chrom_category_aneuploidy == "Partial", "Euploid",
                             ifelse(chrom_category_aneuploidy == "Full", chromosome, "Euploid")))%>%
    group_by(chromosome)%>%
    filter(!all(chromosome_2 == "Euploid"))%>%
    ungroup()%>%
    select(strain, chromosome, chromosome_2)%>%
    pivot_wider(names_from = chromosome,
                values_from = chromosome_2)%>%
    column_to_rownames("strain")

Full tree plots

Tree

Code
tree <- read.tree(merged_tree_path)

Remove tips that are not in metadata$strain

Code
tree <- drop.tip(tree, setdiff(tree$tip.label, metadata$strain))

Lineage nodes

Get the node number of the Most Recent Common Ancestor of each lineage

Code
VNI_node <- getMRCA(tree, c("Tu241-1","UI_31647-2"))
VNII_node <- getMRCA(tree, c("C2","C12"))
VNBI_node <- getMRCA(tree, c("Tu229-1","Ftc267-2"))
VNBII_node <- getMRCA(tree, c("MW-RSA3321","MW-RSA3179"))

VNIa4_node <- getMRCA(tree, c("04CN-30-008","UI_31647-2"))
VNIa5_node <- getMRCA(tree, c("BMD852","14936_1#45"))
VNIa93_node <- getMRCA(tree, c("04CN-65-080","04CN-65-002"))
VNIa32_node <- getMRCA(tree, c("BMD942","BMD2801"))
VNIaX_node <- getMRCA(tree, c("Bt48","04CN-63-007"))
VNIaY_node <- getMRCA(tree, c("04CN-65-073","Bt138"))

VNIa_node <- getMRCA(tree, c("04CN-30-008","BMD852"))
VNIb_node <- getMRCA(tree, c("04CN-65-096","MW-RSA722"))
VNIc_node <- getMRCA(tree, c("Bt20","Bt11"))
Code
nodes_lineages <- data.frame(
    lineage = c("VNI", "VNII", "VNBI", "VNBII"),
    mrca = c(VNI_node, VNII_node, VNBI_node, VNBII_node))

nodes_sublineages <- data.frame(
    sublineage = c("VNII", "VNBII", "VNBI", "VNIb","VNIc", "VNIa"),
    mrca = c(VNII_node, VNBII_node, VNBI_node, VNIb_node, VNIc_node, VNIa_node),
    shading = c("gray30", "gray60","gray30", "gray60","gray30", "gray60"))

nodes_vnisublineages <- data.frame(
    sublineage = c("VNIb","VNIc", "VNIa"),
    mrca = c(VNIb_node, VNIc_node, VNIa_node),
    shading = c("gray90", "gray70","gray90"))

nodes_vniasublineages <- data.frame(
    sublineage = c("VNIb", "VNIc", "VNIa-4", "VNIa-32", "VNIa-93", "VNIa-5"),
    mrca = c(VNIb_node, VNIc_node, VNIa4_node, VNIa32_node, VNIa93_node, VNIa5_node),
    shading = c("gray70", "gray90","gray70", "gray90","gray70", "gray90"))
Code
sublineage_shading <- nodes_sublineages$shading
names(sublineage_shading) <- nodes_sublineages$sublineage

vnisublineage_shading <- nodes_vnisublineages$shading
names(vnisublineage_shading) <- nodes_vnisublineages$sublineage

vniasublineage_shading <- nodes_vniasublineages$shading
names(vniasublineage_shading) <- nodes_vniasublineages$sublineage

Color palettes

Code
chrom_colors <- brewer.pal(7, "Dark2")
names(chrom_colors) <- c("chr01", "chr04",
                         "chr06", "chr09", 
                         "chr12","chr13", "chr14")
chrom_dup_colors <- c(chrom_colors, "Euploid" = "grey93")

countries_final <- levels(droplevels(country[rownames(country) %in% tree$tip.label, ]))
Code
category_size_colors <-c("#8E0152","#2171B5", "#DEEBF7","#F7FBFF" )
names(category_size_colors) <- levels(chrom_metrics$chrom_category_size)

category_aneuploidy_colors <-c("#8E0152","#2171B5", "#DEEBF7" )
names(category_aneuploidy_colors) <- levels(chrom_metrics$chrom_category_aneuploidy)

Continent, source, lineage and sublineage labels

Full duplications

Code
m <- ggtree(tree, 
        ladderize = TRUE,
        layout = "circular", 
        branch.length = "none",
        size = 0.1) %<+%  metadata +
    geom_tree(size = 0.1)+
    geom_text(aes(label = nodes_sublineages$sublineage[match(node, nodes_sublineages$mrca)]), 
                        size = 2, , fontface = "bold",
                        hjust = 1.25, vjust = -0.5)

m1 <- gheatmap(m, continent, width=.05, colnames=FALSE) +
        scale_fill_manual(values = continent_colors, name="Continent",
            na.translate = FALSE, limits = names(continent_colors))+
        guides(fill = guide_legend(order = 1, ncol = 2))+
        new_scale_fill()

m2 <- gheatmap(m1, source, width=.05, colnames=FALSE, offset=2.3) +
        scale_fill_manual(values = source_colors, name="Source", na.translate = FALSE)+
        guides(fill = guide_legend(order = 2))+
        new_scale_fill()
m3 <- gheatmap(m2, dup_chroms, width=.32, colnames = FALSE, offset=4.7) +
    scale_fill_manual(values = chrom_dup_colors, 
                    name="Duplicated\nchromosomes", 
                    na.translate = FALSE )+
    guides(fill = guide_legend(order = 3, ncol = 2))+
    theme(legend.position = "bottom",
    legend.direction = "vertical")
m3

Code
ggsave(tree_merged_duplications_path1, m3, height = 8, width = 6.5, units = "in", dpi = 1000)

Coverage percent of duplications

Code
m3 <- gheatmap(m2, dup_coverage, width=.8, colnames = FALSE, offset=4.7) +
    scale_fill_viridis_c(name = "% Coverage\nDuplicated", 
                        direction = -1, na.value = "white", 
                        option = "mako", limits = c(0,100))+
    theme(legend.position = "bottom",
    legend.direction = "vertical")
m3

Code
ggsave(tree_merged_duplications_path2, m3, height = 8, width = 6.5, units = "in", dpi = 1000)

Aneuploidy category of duplications

Code
m3 <- gheatmap(m2, dup_category_aneuploidy,
         colnames = TRUE,
         font.size = 2,
         colnames_angle = 270,
         colnames_position = "top",
         offset=4.7) +
    scale_fill_manual(name = "Category",values = category_aneuploidy_colors,
         na.value = "white", limits = levels(chrom_metrics$chrom_category_aneuploidy))+
    guides(fill = guide_legend(order = 3))+
    theme(legend.position = "bottom",
    legend.direction = "vertical")
m3

Code
ggsave(tree_merged_duplications_path3, m3, height = 8, width = 6.5, units = "in", dpi = 1000)

Size category of duplications

Code
m3 <- gheatmap(m2, dup_category_size, width=.8, colnames = FALSE, offset=4.7) +
    scale_fill_manual(name = "Category",values = category_size_colors,
         na.value = "white", limits = levels(chrom_metrics$chrom_category_size))+
    guides(fill = guide_legend(order = 3))+
    theme(legend.position = "bottom",
    legend.direction = "vertical")
m3

Code
ggsave(tree_merged_duplications_path4, m3, height = 8, width = 6.5, units = "in", dpi = 1000)

Continent, source, lineage and sublineage labels, tiplabels

Full duplications

Code
m <- ggtree(tree, 
        ladderize = TRUE,
        layout = "circular", 
        branch.length = "none",
        size = 0.1) %<+%  metadata +
    geom_tree(size = 0.1)+
    geom_text(aes(label = nodes_sublineages$sublineage[match(node, nodes_sublineages$mrca)]), 
                        size = 2, , fontface = "bold",
                        hjust = 1.25, vjust = -0.5)+
    geom_tiplab(color = "black", size = 0.3, offset = 0.01)

m1 <- gheatmap(m, continent, width=.05, colnames=FALSE,offset=3.7 ) +
        scale_fill_manual(values = continent_colors, name="Continent",
            na.translate = FALSE, limits = names(continent_colors))+
        guides(fill = guide_legend(order = 1, ncol = 2))+
        new_scale_fill()

m2 <- gheatmap(m1, source, width=.05, colnames=FALSE, offset=6) +
        scale_fill_manual(values = source_colors, name="Source", na.translate = FALSE)+
        guides(fill = guide_legend(order = 2))+
        new_scale_fill()
m3 <- gheatmap(m2, dup_chroms, width=.32, colnames = FALSE, offset=8) +
    scale_fill_manual(values = chrom_dup_colors, 
                    name="Duplicated\nchromosomes", 
                    na.translate = FALSE )+
    guides(fill = guide_legend(order = 3, ncol = 2))+
    theme(legend.position = "bottom",
    legend.direction = "vertical")
m3

Code
ggsave(tree_merged_duplications_path5, m3, height = 8, width = 6.5, units = "in", dpi = 1000)

Coverage percent of duplications

Code
m3 <- gheatmap(m2, dup_coverage, width=.8, colnames = FALSE, offset=8) +
    scale_fill_viridis_c(name = "% Coverage\nDuplicated", 
                        direction = -1, na.value = "white", 
                        option = "mako", limits = c(0,100))+
    theme(legend.position = "bottom",
    legend.direction = "vertical")
m3

Code
ggsave(tree_merged_duplications_path6, m3, height = 8, width = 6.5, units = "in", dpi = 1000)

Aneuploidy category of duplications

Code
m3 <- gheatmap(m2, dup_category_aneuploidy, colnames = FALSE, offset=8) +
    scale_fill_manual(name = "Category",values = category_aneuploidy_colors,
         na.value = "white", limits = levels(chrom_metrics$chrom_category_aneuploidy))+
    guides(fill = guide_legend(order = 3))+
    theme(legend.position = "bottom",
    legend.direction = "vertical")
m3

Code
ggsave(tree_merged_duplications_path7, m3, height = 8, width = 6.5, units = "in", dpi = 1000)

Size category of duplications

Code
m3 <- gheatmap(m2, dup_category_size, width=.8, colnames = FALSE, offset=8) +
    scale_fill_manual(name = "Category",values = category_size_colors,
         na.value = "white", limits = levels(chrom_metrics$chrom_category_size))+
    guides(fill = guide_legend(order = 3))+
    theme(legend.position = "bottom",
    legend.direction = "vertical")
m3

Code
ggsave(tree_merged_duplications_path8, m3, height = 8, width = 6.5, units = "in", dpi = 1000)

Small tree plots

Reduce tree

Code
keep_strains <- chrom_metrics %>%
    filter(cnv == "duplication", chrom_category_aneuploidy %in% c("Partial", "Full"))%>%
    pull(strain)
tree_dups <- drop.tip(tree, setdiff(tree$tip.label, keep_strains))
sublineage <- sublineage %>%
                filter(rownames(.) %in% keep_strains)%>%
                droplevels()

Lineage nodes

Get the node number of the Most Recent Common Ancestor of each lineage

Code
VNI_node <- getMRCA(tree_dups, c("Bt139","AD4-92a"))
VNII_node <- getMRCA(tree_dups, c("8-1","C12"))
VNBI_node <- getMRCA(tree_dups, c("PMHc1001.ENR","NRHc5045.ENR.CLIN.ISO"))
VNBII_node <- getMRCA(tree_dups, c("Bt109","Bt34"))

VNIa4_node <- getMRCA(tree_dups, c("20427_3#26","20427_4#13"))
VNIa5_node <- getMRCA(tree_dups, c("Bt139","Bt141"))
VNIa93_node <- getMRCA(tree_dups, c("04CN-64-024","04CN-64-011"))
VNIa32_node <- getMRCA(tree_dups, c("04CN-65-072","In2632"))

VNIa_node <- getMRCA(tree_dups, c("20427_3#26", "Bt139"))
VNIb_node <- getMRCA(tree_dups, c("AD4-92a","MW-RSA6134"))
VNIc_node <- getMRCA(tree_dups, c("LP-RSA3042","PMHc1031A.ENR.INI.LP"))
Code
nodes_lineages <- data.frame(
    lineage = c("VNI", "VNII", "VNBI", "VNBII"),
    mrca = c(VNI_node, VNII_node, VNBI_node, VNBII_node),
        labels = c("","","",""))


nodes_sublineages <- data.frame(
    sublineage = c("VNII", "VNBII", "VNBI", "VNIb","VNIc", "VNIa"),
    mrca = c(VNII_node, VNBII_node, VNBI_node, VNIb_node, VNIc_node, VNIa_node),
    shading = c("gray70", "gray90","gray70", "gray90","gray70", "gray90"),
    labels = c("","","","","",""))

nodes_vnisublineages <- data.frame(
    sublineage = c("VNIb","VNIc", "VNIa"),
    mrca = c(VNIb_node, VNIc_node, VNIa_node),
    shading = c("gray90", "gray70","gray90"))

nodes_vniasublineages <- data.frame(
    sublineage = c("VNIb", "VNIc", "VNIa-4", "VNIa-32", "VNIa-93", "VNIa-5"),
    mrca = c(VNIb_node, VNIc_node, VNIa4_node, VNIa32_node, VNIa93_node, VNIa5_node),
    shading = c("gray70", "gray90","gray70", "gray90","gray70", "gray90"))
Code
sublineage_shading <- nodes_sublineages$shading
names(sublineage_shading) <- nodes_sublineages$sublineage

vnisublineage_shading <- nodes_vnisublineages$shading
names(vnisublineage_shading) <- nodes_vnisublineages$sublineage

vniasublineage_shading <- nodes_vniasublineages$shading
names(vniasublineage_shading) <- nodes_vniasublineages$sublineage
Code
countries_final <- levels(droplevels(country[rownames(country) %in% tree_dups$tip.label, ]))

Dataset, country, duplications, hilight and cladelabs, tiplabels

Code
m <- ggtree(tree_dups, 
        ladderize = TRUE,
        layout = "rectangular", 
        branch.length = "none",
        size = 0.1) %<+%  metadata +
    geom_tiplab(color = "black", size = 1.2, offset = 0.1)+
    geom_text(aes(label = nodes_lineages$lineage[match(node, nodes_lineages$mrca)]), 
                        size = 2, , fontface = "bold",
                        hjust = 1.25, vjust = -0.5)+
    geom_hilight(data=nodes_vnisublineages, 
        aes(node=mrca, fill=sublineage), alpha = 0.8)+
        scale_fill_manual(name = "Sublineage", values = vnisublineage_shading)+
    guides(fill = FALSE)+
    new_scale_fill()+
    geom_tree(size = 0.1)+
    geom_tippoint(aes(color = dataset), shape = 18,
                size = 1.5)+
    scale_color_manual(name = "Dataset", values = dataset_colors)+
    guides(color = guide_legend(override.aes = list(size = 5), order = 1))

m1 <- gheatmap(m, country, width=.03, colnames=FALSE, offset=2.3) +
        scale_fill_manual(values = country_colors, name="Country",
            na.translate = FALSE, limits = countries_final)+
        guides(fill = guide_legend(order = 2, ncol = 2))+
        new_scale_fill()+
    theme(legend.position = "bottom",
    legend.direction = "vertical")

Aneuploidy category of duplications

Code
m3 <- gheatmap(m1, dup_category_aneuploidy, width=.25, colnames = TRUE,
                colnames_position = "top",
                colnames_angle = 90,
                colnames_offset_y = 2,
                font.size = 1.5, offset=2.8) +
    ylim(0,65)+
    scale_fill_manual(name = "Category",values = category_aneuploidy_colors,
         na.value = "white", limits = names(category_aneuploidy_colors))+
    guides(fill = guide_legend(order = 5))+
    geom_cladelab(data = nodes_vnisublineages, 
                mapping = aes(node = mrca, label = sublineage),
                align = TRUE, face = "bold",
                fontsize = 3,
                angle = 0, offset = 6.3)+
    theme(legend.position = "bottom",
    legend.direction = "vertical")
m3

Code
ggsave(tree_merged_duplications_small1, m3, height = 5, width = 6.5, units = "in", dpi = 900)

Lineage-sublineage in nodelabels

Code
m <- ggtree(tree_dups, 
        ladderize = TRUE,
        layout = "rectangular", 
        branch.length = "none",
        size = 0.4) %<+%  metadata +
    geom_text(aes(label = nodes_lineages$lineage[match(node, nodes_lineages$mrca)]), 
                        size = 3, , fontface = "bold",
                        hjust = 1.25, vjust = -0.5)

Aneuploidy category of duplications

Code
m3 <- gheatmap(m, dup_category_aneuploidy, width=1, colnames = TRUE,
                colnames_position = "top",
                colnames_angle = 90,
                colnames_offset_y = 3,
                font.size = 3, offset=0) +
    ylim(0,65)+
    labs(title = "b)")+
    scale_fill_manual(name = "",values = category_aneuploidy_colors,
         na.value = "white", limits = names(category_aneuploidy_colors))+
    theme(
      legend.position = c(0.032, 0.96),
      legend.justification = c(0, 1),
      legend.direction = "vertical"
    )
m3

Code
ggsave(tree_merged_duplications_small2, m3, height = 4.5, width = 6.5, units = "in", dpi = 900)

Aneuploidy category of duplications

Code
m3 <- gheatmap(m, dup_category_aneuploidy, width=0.7, colnames = TRUE,
                colnames_position = "top",
                colnames_angle = 90,
                colnames_offset_y = 3,
                font.size = 3, offset=0) +
    ylim(0,65)+
    labs(title = "b)")+
    scale_fill_manual(name = "",values = category_aneuploidy_colors,
         na.value = "white", limits = names(category_aneuploidy_colors))+
    geom_cladelab(data = nodes_lineages, 
                mapping = aes(node = mrca, label = labels),
                align = TRUE, face = "bold",
                fontsize = 3,
                angle = 0, offset = 10)+
    theme(
      legend.position = c(0.032, 0.96),
      legend.justification = c(0, 1),
      legend.direction = "vertical"
    )
m3

Code
ggsave(tree_merged_duplications_small3, m3, height = 4.5, width = 6.5, units = "in", dpi = 900)

Aneuploidy category of duplications with pallete of barplot

Code
category_aneuploidy_colors2 <-brewer.pal(11, "BrBG")[c(11,8,7)]
names(category_aneuploidy_colors2) <- levels(chrom_metrics$chrom_category_aneuploid)
Code
m3 <- gheatmap(m, dup_category_aneuploidy, width=1, colnames = TRUE,
                colnames_position = "top",
                colnames_angle = 90,
                colnames_offset_y = 3,
                font.size = 3, offset=0) +
    ylim(0,65)+
    labs(title = "b)")+
    scale_fill_manual(name = "",values = category_aneuploidy_colors2,
         na.value = "white", limits = names(category_aneuploidy_colors2))+
    theme(
      legend.position = c(0.032, 0.96),
      legend.justification = c(0, 1),
      legend.direction = "vertical"
    )
m3

Code
ggsave(tree_merged_duplications_small4, m3, height = 4.5, width = 6.5, units = "in", dpi = 900)
Code
m3 <- gheatmap(m, dup_category_aneuploidy, width=0.7, colnames = TRUE,
                colnames_position = "top",
                colnames_angle = 90,
                colnames_offset_y = 3,
                font.size = 3, offset=0) +
    ylim(0,65)+
    labs(title = "b)")+
    scale_fill_manual(name = "",values = category_aneuploidy_colors2,
         na.value = "white", limits = names(category_aneuploidy_colors2))+
    geom_cladelab(data = nodes_lineages, 
                mapping = aes(node = mrca, label = labels),
                align = TRUE, face = "bold",
                fontsize = 3,
                angle = 0, offset = 10)+
    theme(
      legend.position = c(0.032, 0.96),
      legend.justification = c(0, 1),
      legend.direction = "vertical"
    )
m3

Code
ggsave(tree_merged_duplications_small5, m3, height = 4.5, width = 6.5, units = "in", dpi = 900)

Lineage and sublineage in nodelables, tiplabels

Code
m <- ggtree(tree_dups, 
        ladderize = TRUE,
        layout = "rectangular", 
        branch.length = "none",
        size = 0.2) %<+%  metadata +
    geom_tiplab(color = "black", size = 1.5, offset = 0.01)+
    geom_text(aes(label = nodes_lineages$lineage[match(node, nodes_lineages$mrca)]), 
                        size = 2, fontface = "bold",
                        hjust = 1.1, vjust = -0.5)

Coverage percent of duplications

Code
m3 <- gheatmap(m, dup_coverage, width=.7, colnames = TRUE,
                colnames_position = "top",
                colnames_angle = 90,
                colnames_offset_y = 2,
                font.size = 2.5,
                offset=3) +
    ylim(0,66)+
    scale_fill_viridis_c(name = "% Coverage\nDuplicated", 
                        direction = -1, na.value = "white", 
                        option = "mako", limits = c(0,100))+
    theme(
      legend.position = c(0.032, 0.96),
      legend.justification = c(0, 1),
      legend.direction = "vertical",
    legend.text=element_text(size=7),
    legend.title=element_text(size=9),
    legend.key.size = unit(0.4, unit = "cm")
    )
m3

Code
ggsave(tree_merged_duplications_small6, m3, height = 4.5, width = 6.5, units = "in", dpi = 900)

Aneuploidy category of duplications

Code
m3 <- gheatmap(m, dup_category_aneuploidy, width=0.7, colnames = TRUE,
                colnames_position = "top",
                colnames_angle = 90,
                colnames_offset_y = 2,
                font.size = 2.5,
                offset=3) +
    ylim(0,66)+
    scale_fill_manual(name = "",values = category_aneuploidy_colors,
         na.value = "white", limits = names(category_aneuploidy_colors))+
    theme(
      legend.position = c(0.032, 0.96),
      legend.justification = c(0, 1),
      legend.direction = "vertical",
    legend.text=element_text(size=7),
    legend.title=element_text(size=9),
    legend.key.size = unit(0.4, unit = "cm")
    )
m3

Code
ggsave(tree_merged_duplications_small7, m3, height = 4.5, width = 6.5, units = "in", dpi = 900)

Size category of duplications

Code
m3 <- gheatmap(m, dup_category_size, width=.7, colnames = TRUE,
                colnames_position = "top",
                colnames_angle = 90,
                colnames_offset_y = 2,
                font.size = 2.5,
                offset=3) +
    ylim(0,66)+
    scale_fill_manual(name = "",values = category_size_colors,
         na.value = "white", limits = names(category_size_colors))+
    theme(
      legend.position = c(0.032, 0.96),
      legend.justification = c(0, 1),
      legend.direction = "vertical",
    legend.text=element_text(size=7),
    legend.title=element_text(size=9),
    legend.key.size = unit(0.4, unit = "cm")
    )
m3

Code
ggsave(tree_merged_duplications_small8, m3, height = 4.5, width = 6.5, units = "in", dpi = 900)